﻿using DGSFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Framework
{
    public partial class StockAnalyze1 : System.Web.UI.Page
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (User.Identity.IsAuthenticated)
                {
                    var language = "vi-VN";
                    if (Session["language"] != null)
                        language = Session["language"].ToString();
                    hdLanguage.Value = language;
                    var user = Membership.GetUser();
                    var roles = Roles.GetRolesForUser();
                    var userID = new Guid(user.ProviderUserKey.ToString());
                    lblMemberEmail.Text = user.Email;
                    List<dgs_Favorite> favList = null;
                    if (user.UserName.Equals("MasterDK") )
                    {
                        checkBt.Visible = pnCheckBt.Visible = true;
                        var memAdmin = Membership.GetUser("uyenadmin");
                        var userIDAdmin = new Guid(memAdmin.ProviderUserKey.ToString());
                        favList = entity.dgs_Favorite.Where(d => d.udv_Users.UserID == userID || d.udv_Users.UserID == userIDAdmin).ToList();
                    }
                    else if(user.UserName.Equals("uyenadmin")){
                        checkBt.Visible = pnCheckBt.Visible = true;
                        var memAdmin = Membership.GetUser("MasterDK");
                        var userIDAdmin = new Guid(memAdmin.ProviderUserKey.ToString());
                        favList = entity.dgs_Favorite.Where(d => d.udv_Users.UserID == userID || d.udv_Users.UserID == userIDAdmin).ToList();
                    }
                    else
                        favList = entity.dgs_Favorite.Where(d => d.udv_Users.UserID == userID).ToList();
                    ltMenuContext.Text = Globals.GetMenuContext(user.UserName, MenuType.allstock.ToString(), roles, favList);
                    ltDropdownMenu.Text = Globals.GetDropdownMenu(user.UserName, roles, favList);                   
                }
                else
                {
                    string returnUrl = Request.Url.PathAndQuery;
                    Response.Redirect("/login.aspx?url=" + returnUrl);
                }
            }
        }

        public void SetLanguage(object sender, EventArgs e)
        {
            if (Globals.CheckRewriteUrl() == false)
            {
                Session["language"] = ((ImageButton)sender).Attributes["rel"].ToString();
                Response.Redirect(Request.RawUrl);
            }
            else
            {
                Session["language"] = ((ImageButton)sender).Attributes["rel"].ToString();
                if (Request.Url.LocalPath.ToLower().Contains("default.aspx"))
                {
                    Response.Redirect(ResolveUrl("~/"));
                }
                else
                {
                    if (Request.ServerVariables["HTTP_X_REWRITE_URL"] != null)
                        Response.Redirect(Request.ServerVariables["HTTP_X_REWRITE_URL"]);
                    else
                        Response.Redirect(Request.RawUrl);
                }
            }
        }
    }
}